//
//  UpdateController.swift
//  appstore
//
//  Created by 王雷 on 2019/11/2.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class UpdateController: BaseViewController,UITableViewDataSource,UITableViewDelegate {
    
    let UpdateTableViewCellIdentifier = "UpdateTableViewCellIdentifier"
        
    let data:[UpdateModel] = [
        UpdateModel(iconImageStr:"logo_broadcast",appName:"Huajiao Live",updateStr:"Today",version:"2.0.0",size:"35.7",content:"[Play] Music radio broadcasting page revision, more immersed in music exploration \n\n[Mine] Rewriting sets the position of the night mode \n\n[Radio] Let's go with DJ and get up!",showMore:false),
        UpdateModel(iconImageStr:"logo_weibo",appName:"Sina Weibo",updateStr:"Today",version:"5.3.3",size:"32.5",content:"-Performance improvements and bug fixed",showMore:false),
        UpdateModel(iconImageStr:"logo_smile",appName:"Sougou Input",updateStr:"Yesterday",version:"2.1.1",size:"42.2",content:"Fix bug and to be better for you",showMore:false),
        UpdateModel(iconImageStr:"logo_car",appName:"Guazi Car",updateStr:"Yesterday",version:"1.5.0",size:"28.0",content:"Sometimes, a polish is all you need. No big chages, just a shine",showMore:false),
        UpdateModel(iconImageStr:"logo_jump",appName:"Fly Chat",updateStr:"2019/08/02",version:"1.5.6",size:"33.0",content:"This update includes bug fixed and user interface improvements",showMore:false),
    ]
    
    lazy var updateTableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.initView()
    }
    
    func initView() -> Void {
        self.navigationController?.navigationBar.prefersLargeTitles = true // 导航栏大标题
        super.changeNavigationBarBackground()
        self.navigationItem.title = "Update";
        self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
        super.setNavigationBarUserItem(target: self, action: #selector(userClickAction))
        
        // 滚动时导航栏b由透明变不透明
        // https://segmentfault.com/p/1210000009806928/read
        
        // tableview
        updateTableView = UITableView(frame: CGRect.zero, style: .plain)
        updateTableView.dataSource = self
        updateTableView.delegate = self
        updateTableView.estimatedRowHeight = 200.0
        updateTableView.rowHeight = UITableView.automaticDimension
        self.view.addSubview(self.updateTableView)
        updateTableView.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalTo(self.view)
        }
        updateTableView.register(UpdateCell.self, forCellReuseIdentifier: UpdateTableViewCellIdentifier)
        
        // tableHeaderView
        let tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: ScreenWidth, height: 40.0))
        self.updateTableView.tableHeaderView = tableHeaderView
        
        let titleLabel = UILabel()
        titleLabel.text = "Pending"
        titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
        tableHeaderView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(tableHeaderView).offset(20.0)
            make.top.equalTo(tableHeaderView).offset(0.0)
            make.width.equalTo(150.0)
            make.height.equalTo(40.0)
        }
        
        let updateAllButton = UIButton(type: .custom)
        updateAllButton.frame = CGRect(x: (self.view.frame.size.width - 150.0), y: 0.0, width: 150.0, height: 40.0)
        updateAllButton.setTitle("Update All", for: .normal)
        updateAllButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        updateAllButton.setTitleColor(UIColor.blue, for: .normal)
        updateAllButton.addTarget(self, action: #selector(updateAllClickAction(button:)), for: .touchUpInside)
        tableHeaderView.addSubview(updateAllButton)
        updateAllButton.snp.makeConstraints { (make) in
            make.right.equalTo(tableHeaderView).offset(0.0)
            make.width.equalTo(150.0)
            make.height.equalTo(40.0)
        }
    }
    
    @objc func userClickAction() -> Void {
        print("User ----")
    }
    
    @objc func updateAllClickAction(button:UIButton) -> Void {
        print("Update All")
    }
    
    // UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: UpdateTableViewCellIdentifier) as! UpdateCell
        cell.showMoreClosure = {[weak self](param) in
            self!.data[indexPath.row].showMore = true
            self!.updateTableView.reloadRows(at: [indexPath], with: .none)
            print(param)
        }
        cell.model = self.data[indexPath.row]
        return cell
    }
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
